package com.kaifamiao.www.demo.Do21;

import java.util.Scanner;

public class De07 {
    public static void main(String[] args) {
        //输入数字，选择在输入的数字中增添或删除数字
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[5];
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入第" + (i + 1) + "个数字");
            arr[i] = sc.nextInt();
        }
        //输入数字，输入要进行的操作；1增加 2删除
        System.out.println("请输入要进行的操作  1，增添  2，删除");
        int type = sc.nextInt();
if(type == 1){
    System.out.println("请输入要进行操作的数字");
    int num = sc.nextInt();

    int index = -1;
    while (!(index >= 0 && index <= 5)) {
        System.out.println("请输入想要添加元素的下标(0 ~ 5):");
        index = sc.nextInt();
    }
        int[] newArr = new int[arr.length + 1];
        System.arraycopy(arr, 0, newArr, 0, arr.length);
        System.arraycopy(newArr, index, newArr, index + 1, newArr.length - index -1);
        newArr[index] = num;
        for (int i = 0; i < newArr.length; i++) {
            System.out.println(newArr[i]);
        }
    }
    //删除元素
    else if(type == 2){
    System.out.println("请输入删除元素的下标（0 ~ 4）");
    int index = sc.nextInt();
    //
   //System.arraycopy(arr,index+1,arr,index,arr.length-index-1);
    int[] newArrs = new int[arr.length - 1];
    for(int i = 0,j = 0;i < arr.length-1;i++){
        if(i == index){
            continue;
        }
        newArrs[j++] = arr[i];
    }
        for(int i = 0;i < newArrs.length;i++){
            System.out.println(newArrs[i]);
        }
    }
    else{
    System.out.println("输入错误" );
    }
    }
}
